﻿using UnityEngine;
using System.IO;
using System.Text;


public static class DataLoader
{
    private static string dataPath = Application.dataPath;


    public static void WariteData(string dataName,string json)
    {
        var filePath = dataPath+"/"+dataName+".txt";
        var file = File.Open(filePath,FileMode.OpenOrCreate);

        byte[] dataBytes = Encoding.UTF8.GetBytes(json);
        file.Write(dataBytes,0,dataBytes.Length);

        file.Close();
    }

    public static string LoadData(string dataName)
    {
        var filePath = dataPath+"/"+dataName+".txt";

        var file = File.Open(filePath,FileMode.OpenOrCreate);
        var len = CheckByteArrayLen(file.Length);

        byte[] data = new byte[file.Length];
        file.Read(data,0,data.Length);
        file.Close();

        string result = Encoding.UTF8.GetString ( data );
        return result;
    }

    private static long CheckByteArrayLen(long length)
    {
        if(length%1024==0)
            return length;
        else
        {
            var remain = length%1024;
            var format = 1024 - remain;

            return length+format;
        }
    }
}
